package com.yx.cloud.receiver;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.iot.common.utils.LogUtils;
import com.iot.product.sweeper.bean.Robot;
import com.iot.sweeper.AppContext;
import com.yx.cloud.MyApplication;
import com.yx.cloud.R;
import com.yx.cloud.activity.BroadcastReceiverDialog;
import com.yx.cloud.activity.ConnectSuccessActivity;
import com.yx.cloud.activity.MainBottomTabActivity;
import com.yx.cloud.bean.tmall.TmallUnbindDeviceParam;
import com.yx.cloud.product.laser.inxni.INXNIConstant;
import com.yx.cloud.product.laser.yxcloud.X5_MAX;
import com.yx.cloud.utils.AliHttpRequest_Runnable;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.SharedPreferencesList;
import com.yx.cloud.utils.SharedPrefsUtil;
import com.yx.cloud.utils.ThreadUtils;

/**
 * 网络连接的广播
 */
public class GlobalBroadcastReceiver extends BroadcastReceiver {
    private final static String TAG = "GlobalBroadcastReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        // 如果相等的话就说明网络状态发生了变化
        String action = intent.getAction();
        if (action.equals("com.yx.cloud.device.state")) {
            String tag = intent.getStringExtra("tag");
            LogUtils.i("收到 线 广播了。。。。。tag ：" + tag);
            if ("online".equals(tag)) {
                boolean connected = intent.getBooleanExtra("connected", false);
                //LogUtils.i("收到 线 广播了。。。。。22222 : " + connected);
                if (!connected) {
                    if (BroadcastReceiverDialog.instance == null) {
                        //LogUtils.i("收到 线 广播了。。。。。333333333333333333333: ");
                        Intent_utils.enterIntent(context, BroadcastReceiverDialog.class);
                    }
                } else {
                    if (BroadcastReceiverDialog.instance != null) {
                        BroadcastReceiverDialog.instance.finish();
                        BroadcastReceiverDialog.instance = null;
                    }
                }
            } else {
                LogUtils.i("重置 广播了 收到了");
                //删除天猫设备
                Robot robot = AppContext.get().getCurrentDevice();
                SharedPreferencesList.switchRobotPicture(context, "delete", "", AppContext.get().getDeviceList(), "delete");
                if (X5_MAX.PID.equals(robot.getProductId())) {
                    String tmallDeviceId = robot.getTmalDeviceId();
                    SharedPreferencesList.switchRobotPictureX5(context, "delete", "", null, tmallDeviceId);
                    INXNIConstant.USER_OPEN_ID = SharedPrefsUtil.getValue(context, "TMALL_USER_OPEN_ID", "tmall_user_open_id", "");
                    TmallUnbindDeviceParam param = new TmallUnbindDeviceParam();
                    param.setUserOpenId(INXNIConstant.USER_OPEN_ID);
                    param.setDeviceId(tmallDeviceId);
                    AliHttpRequest_Runnable runnable = new AliHttpRequest_Runnable();
                    runnable.initialization("unbind_device", param);
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                if (BroadcastReceiverDialog.instance != null) {
                    BroadcastReceiverDialog.instance.finish();
                    BroadcastReceiverDialog.instance = null;
                }
                //重置广播
                MyApplication.recvActive = false;
                Intent_utils.enterIntent(context, MainBottomTabActivity.class);
            }
        }
    }
}



